﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using System.IO;
using System.Runtime.InteropServices;

namespace McDonalds_v1._0
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]



    public class WebService1 : System.Web.Services.WebService
    {       

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public XmlDocument FILEnaarXML()
        {
            XmlDocument mijnDocument = new XmlDocument();

            mijnDocument.Load("http://localhost:54805/ScheduleWithoutDTD.xml");

            return(mijnDocument);
        }

        [WebMethod]
        public XmlDocument ProductNaarXML(string PRODUCTNR)
        {
            ProductDS pds = new ProductDS();
            ProductDSTableAdapters.PRODUCTTableAdapter pta = new ProductDSTableAdapters.PRODUCTTableAdapter();

            XmlDocument productDocument = new XmlDocument();
            
            pta.Fill(pds.PRODUCT);
            pta.GetData();

            productDocument.LoadXml(pds.GetXml());
                       

            // Maak selectie m.b.v. XPath
            XmlNodeList selectieXmlNodeList = productDocument.SelectNodes("//ProductDS/PRODUCT[PRODUCTNR = "+PRODUCTNR+"]");

            // Maak een nieuw XmlDocument
            XmlDocument outputXmlDocument = new XmlDocument();

            // Voeg een root-element toe aan het nieuwe XmlDocument
            outputXmlDocument.AppendChild(outputXmlDocument.CreateElement("output"));

            // Loop door de XmlNodes in de selectie
            foreach (XmlNode selectedXmlNode in selectieXmlNodeList)
            {
                // Maak een kopie van iedere geselecteerde XmlNode
                XmlNode newXmlNode = outputXmlDocument.ImportNode(selectedXmlNode, true);

                // Voeg deze kopie toe aan het root element (root = FirstChild)
                outputXmlDocument.FirstChild.AppendChild(newXmlNode);
            }

            return (outputXmlDocument);

        }


        [WebMethod]
        public XmlDocument CallPerProduct()
        {
            McDonaldsDataContext db = new McDonaldsDataContext();

            // Query ingredienten ophalen
            XElement xml = new XElement("ProductCalorien",
                             from p in db.PRODUCTs
                             join ps in db.PRODUCTINGREDIENTENs on p.PRODUCTNR equals ps.PRODUCTNR
                             join i in db.INGREDIENTs on ps.INGREDIENTNR equals i.INGREDIENTNR
                             group new { p.NAAM, i.CALORIEN, p.PRODUCTNR } by new { p.NAAM } into g
                             select new XElement("Product",
                                 new XElement("id", g.First().PRODUCTNR),
                                 new XElement("Naam", g.Key.NAAM),
                                 new XElement("Calorien", g.Sum(x => x.CALORIEN))
                             )
                         );


            XmlReader xmlReader = xml.CreateReader();
            XmlDocument productenXML = new XmlDocument();
            productenXML.Load(xmlReader);

            return productenXML;
         
        }

        [WebMethod]
        public XmlDocument top10BesteldeProducten()
        {
            McDonaldsDataContext db = new McDonaldsDataContext();

            // Query ingredienten ophalen
            XElement xml = new XElement("Producten",
                             from p in db.PRODUCTs.Take(10)
                             from po in db.PRODUCTORDERs
                             where p.PRODUCTNR == po.PRODUCTNR
                             group po by p into g
                             orderby g.Sum(x=> x.AANTAL) descending                             
                             select new XElement("Product",
                                 new XElement("id", g.First().PRODUCTNR),
                                 new XElement("Naam", g.Key.NAAM),
                                 new XElement("Aantal", g.Sum(x => x.AANTAL))
                             )                             
                         );

            XmlReader xmlReader = xml.CreateReader();
            XmlDocument productenXML = new XmlDocument();
            productenXML.Load(xmlReader);

            return productenXML;
        }

        [WebMethod]
        public XmlDocument productXMLtoFSXML()
        {
            XmlDocument productXML = new XmlDocument();
            productXML.Load("http://localhost:54805/WebService1.asmx/CallPerProduct");

            string xsltURL = Server.MapPath("XSLT/XSLT_FS.xsl");

            XmlTextReader textR = new XmlTextReader(xsltURL);

            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(textR);

            MemoryStream ms = new MemoryStream();
            xslt.Transform(productXML, null, ms);

            XmlDocument FSXML = new XmlDocument();
            ms.Position = 0;

            FSXML.Load(ms);
            ms.Close();

            return FSXML;
        }

        [WebMethod]
        public XmlDocument productXMLtoRSSXML()
        {
            XmlDocument productXML = new XmlDocument();
            productXML.Load("http://localhost:54805/WebService1.asmx/CallPerProduct");

            string xsltURL = Server.MapPath("XSLT/XSLT_RSS.xsl");

            XmlTextReader textR = new XmlTextReader(xsltURL);

            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(textR);

            MemoryStream ms = new MemoryStream();
            xslt.Transform(productXML, null, ms);

            XmlDocument RSSXML = new XmlDocument();
            ms.Position = 0;

            RSSXML.Load(ms);
            ms.Close();

            return RSSXML;
        }


        [WebMethod]
        public XmlDocument tagCloudXML()
        {
            XmlDocument xm = new XmlDocument();
            xm.Load("http://localhost:54805/WebService1.asmx/top10BesteldeProducten");

            string f = Server.MapPath("XSLT/XSLT_Wordcloud.xsl");

            XmlTextReader t = new XmlTextReader(f);

            XslCompiledTransform x = new XslCompiledTransform();
            x.Load(t);

            MemoryStream ms = new MemoryStream();
            x.Transform(xm, null, ms);

            XmlDocument ou = new XmlDocument();
            ms.Position = 0;

            ou.Load(ms);
            ms.Close();

            return ou;
        }

        [WebMethod]
        public void transformProductXMLtoFSChartXML()
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load("http://localhost:54805/XSLT/XSLT_FS.xsl");
            xslt.Transform("http://localhost:54805/WebService1.asmx/CallPerProduct", "C:\\Users\\Ramon\\Documents\\Visual Studio 2010\\Projects\\McDonalds_v1.0\\McDonalds_v1.0\\FS_XML\\product_FSXML.xml");
        }

        [WebMethod]
        public XmlDocument CallPerMenu()
        {
            McDonaldsDataContext db = new McDonaldsDataContext();

            // Query ingredienten ophalen
            XElement xml = new XElement("MenuCalorien",
                             from m in db.MENUs
                             join ms in db.MENUSAMENSTELLINGs on m.MENUNR equals ms.MENUNR
                             join p in db.PRODUCTs on ms.PRODUCTNR equals p.PRODUCTNR
                             join ps in db.PRODUCTINGREDIENTENs on p.PRODUCTNR equals ps.PRODUCTNR
                             join i in db.INGREDIENTs on ps.INGREDIENTNR equals i.INGREDIENTNR
                             group new { m.MENUNAAM, i.CALORIEN } by new { m.MENUNAAM } into g
                             select new XElement("Menu",
                                 new XElement("Naam", g.Key.MENUNAAM),
                                 new XElement("Calorien", g.Sum(x => x.CALORIEN))
                             )
                         );


            XmlReader xmlReader = xml.CreateReader();
            XmlDocument productenXML = new XmlDocument();
            productenXML.Load(xmlReader);

            return (productenXML);
        }

        [WebMethod]
        public XmlDocument verkochteProducten(int year, int month = 0)
        {
            McDonaldsDataContext db = new McDonaldsDataContext();

            XElement xml;
            if (month == 0)
            {
                xml = new XElement("ProductOpbrengst",
                            from ord in db.ORDERs
                            join pord in db.PRODUCTORDERs on ord.ORDERNR equals pord.ORDERNR
                            join pro in db.PRODUCTs on pord.PRODUCTNR equals pro.PRODUCTNR
                            join prop in db.PRODUCTPRIJs on pro.PRODUCTNR equals prop.PRODUCTNR
                            where
                              pord.ORDER.DATUM.Year == year
                            group new { pord.PRODUCT, prop, pord } by new
                            {
                                prop.PRODUCT.NAAM
                            } into g
                            select new XElement("Product",
                                new XElement("id", g.First().PRODUCT.PRODUCTNR),
                                new XElement("Naam", g.First().PRODUCT.NAAM),
                                new XElement("Aantal", g.Sum(y => y.pord.AANTAL)),
                                new XElement("Prijs", (System.Double?)g.Sum(x => x.prop.PRIJS * x.pord.AANTAL))
                            )
                        );
            }
            else
            {
                xml = new XElement("ProductOpbrengst",
                           from ord in db.ORDERs
                           join pord in db.PRODUCTORDERs on ord.ORDERNR equals pord.ORDERNR
                           join pro in db.PRODUCTs on pord.PRODUCTNR equals pro.PRODUCTNR
                           join prop in db.PRODUCTPRIJs on pro.PRODUCTNR equals prop.PRODUCTNR
                           where
                             pord.ORDER.DATUM.Year == year &&
                             pord.ORDER.DATUM.Month == month
                           group new { pord.PRODUCT, prop, pord } by new
                           {
                               prop.PRODUCT.NAAM
                           } into g
                           select new XElement("Product",
                               new XElement("id", g.First().PRODUCT.PRODUCTNR),
                               new XElement("Naam", g.First().PRODUCT.NAAM),
                               new XElement("Aantal", g.Sum(y => y.pord.AANTAL)),
                               new XElement("Prijs", (System.Double?)g.Sum(x => x.prop.PRIJS * x.pord.AANTAL))
                           )
                       );
            }

            XmlReader xmlReader = xml.CreateReader();
            XmlDocument productenXML = new XmlDocument();
            productenXML.Load(xmlReader);

            return productenXML;

        }

        [WebMethod]
        public XmlDocument omzetXML(int year, int month = 0)
        {
            XmlDocument xm = new XmlDocument();

            string url = "http://localhost:54805/WebService1.asmx/verkochteProducten?year=" + year.ToString() + "&month=" + month.ToString();


            xm.Load(url);

            string f = Server.MapPath("XSLT/XSLT_FSomzet.xsl");

            XmlTextReader t = new XmlTextReader(f);

            XslCompiledTransform x = new XslCompiledTransform();
            x.Load(t);

            MemoryStream ms = new MemoryStream();
            x.Transform(xm, null, ms);

            XmlDocument ou = new XmlDocument();
            ms.Position = 0;

            ou.Load(ms);
            ms.Close();

            return ou;
        }
    }
}
